<?php
namespace App\Models\Trade\Shop;

use App\Models\Model;

class ShopCountModel extends Model
{

    const TABLENAME = 'tw_trade_shop_count';

    /**
     * 查询一条数据（店铺 ID）
     *
     * @param int $shopId
     * @param array $fields
     * @return \Illuminate\Support\Collection
     */
    public static function findByShopId($shopId, array $fields = [])
    {
        $query = self::dbTable();
        if (count($fields)) {
            $query->select($fields);
        }
        
        return $query->where('shop_id', $shopId)->get();
    }

    /**
     * 统计自增
     *
     * @param string $field            
     * @param int $shopId            
     */
    protected static function inc($field, $shopId)
    {
        $query = self::dbTable();
        
        $number = self::findByShopId($shopId)->count();
        if ($number) {
            return $query->where('shop_id', $shopId)->increment($field);
        } else {
            return $query->insert([
                'shop_id' => $shopId,
                $field => 1
            ]);
        }
    }

    /**
     * 统计自减
     *
     * @param string $field            
     * @param int $shopId            
     */
    protected static function dec($field, $shopId)
    {
        $query = self::dbTable();
        
        $item = self::findByShopId($shopId)->shift();
        if ($item && $item[$field] > 0) {
            return $query->where('shop_id', $shopId)->decrement($field);
        }
        return false;
    }

    /**
     * 统计自减（批量修改）
     *
     * @param string $field
     * @param int $shopIds
     * @param int $userId
     */
    protected static function decByShopIds($field, $shopIds, $userId)
    {
        if (! $shopIds || ! count($shopIds) || ! $userId) {
            return false;
        }
        $where[] = ['dc.user_id', $userId];
        $where[] = ["dcv.$field", '>', 0];
    
        return self::dbTable('dcv')->where($where)
            ->join(ShopCollectionModel::TABLENAME . ' as dc', 'dc.shop_id', '=', 'dcv.shop_id')
            ->whereIn('dcv.shop_id', $shopIds)
            ->decrement("dcv.$field");
    }
    
    /**
     * collection +1
     *
     * @param int $shopId            
     */
    public static function incCollection($shopId)
    {
        return self::inc('collection', $shopId);
    }

    /**
     * collection -1
     *
     * @param int $shopId            
     */
    public static function decCollection($shopId)
    {
        return self::dec('collection', $shopId);
    }

    /**
     * collection -1
     *
     * @param int $shopIds
     * @param int $userId
     */
    public static function decCollectionByShopIds($shopIds, $userId)
    {
        return self::decByShopIds('collection', $shopIds, $userId);
    }
    
    /**
     * supply_pub +1
     *
     * @param int $shopId            
     */
    public static function incSupplyPub($shopId)
    {
        return self::inc('supply_pub', $shopId);
    }

    /**
     * demand_pub +1
     *
     * @param int $shopId            
     */
    public static function incDemandPub($shopId)
    {
        return self::inc('demand_pub', $shopId);
    }
}
